/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ClientMoreDto;
import com.scpii.universal.dto.MoreContentDto;
import com.scpii.universal.hibernate.dao.MoreContentDao;
import com.scpii.universal.hibernate.entity.MoreContentTab;

/**
 * genericWeb
 * @author weny
 * 2012-7-6上午11:00:03
 */
@Repository
public class MoreContentDaoImpl extends BaseDaoImpl<MoreContentTab, Integer> implements
		MoreContentDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MoreContentDto> getAllMoreContentById(int pageId, int viewId) {
		String sql = "SELECT m.int_more_content,m.var_more_name," +
				"m.var_more_content,m.int_more_action_type,m.int_order," +
				"m.int_icon_id,a.var_icon_url " +
				"FROM more_content_tab m " +
				"left join app_icon_tab a ON m.int_icon_id=a.int_icon_id " +
				"WHERE m.int_delete_sign=0 and m.int_view_id=? and m.int_resource_id=?" +
				" order by m.int_order desc";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql).addScalar("INT_MORE_CONTENT",
						Hibernate.INTEGER).addScalar("INT_MORE_ACTION_TYPE",
						Hibernate.INTEGER).addScalar("INT_ORDER",
						Hibernate.INTEGER).addScalar("INT_ICON_ID",
						Hibernate.INTEGER).addScalar("VAR_ICON_URL",
						Hibernate.STRING).addScalar("VAR_MORE_NAME",
						Hibernate.STRING).addScalar("VAR_MORE_CONTENT",
						Hibernate.STRING).setResultTransformer(
						Transformers.aliasToBean(MoreContentDto.class))
				.setParameter(0, viewId).setParameter(1, pageId).list();
	}

	@Override
	public void delete(Integer id) {
		String hql = "update MoreContentTab m set m.intDeleteSign=? where m.intMoreContent=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString()).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(1,
						id).executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ClientMoreDto> listMoreContentByCon(int appId, int type) {
		String sql = "SELECT m.int_more_content,m.var_more_name,"
				+ "m.var_more_content,a.var_title_name FROM more_content_tab m"
				+ ",app_page_tab a where m.int_resource_id=a.int_resource_id and a.int_delete_sign=0"
				+ " and m.int_delete_sign=0 and m.int_more_action_type=? and m.int_app_id=?";
		return getHibernateTemplate().getSessionFactory()
				.getCurrentSession().createSQLQuery(sql)
				.addScalar("INT_MORE_CONTENT", Hibernate.INTEGER)
				.addScalar("VAR_TITLE_NAME", Hibernate.STRING)
				.addScalar("VAR_MORE_NAME", Hibernate.STRING)
				.addScalar("VAR_MORE_CONTENT", Hibernate.STRING)
				.setResultTransformer(
						Transformers.aliasToBean(ClientMoreDto.class))
				.setParameter(0, type).setParameter(1, appId).list();
	}

	@Override
	public void deleteMoreContentByViewId(int viewId) {
		String hql = "update MoreContentTab m set m.intDeleteSign=? where m.intViewId=?";
		getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString()).setParameter(0,
						GenericConstants.DELETE_SIGN_DELETED).setParameter(1,
								viewId).executeUpdate();
	}

}
